package cst.a720yyq.zyfypt_720yyq.Modle;


import android.content.SharedPreferences;

import java.util.List;

import cst.a720yyq.zyfypt_720yyq.Bean.ArticleBean;
import cst.a720yyq.zyfypt_720yyq.Bean.LoginBean;
import cst.a720yyq.zyfypt_720yyq.Bean.ProjectBean;
import cst.a720yyq.zyfypt_720yyq.Bean.SVideoBean;
import cst.a720yyq.zyfypt_720yyq.Bean.TcaseBean;
import cst.a720yyq.zyfypt_720yyq.Bean.TwareBean;
import cst.a720yyq.zyfypt_720yyq.Bean.VideoBean;
import cst.a720yyq.zyfypt_720yyq.Interface.CollectListener;
import cst.a720yyq.zyfypt_720yyq.Interface.ResService;
import cst.a720yyq.zyfypt_720yyq.Interface.ToudouIface;
import cst.a720yyq.zyfypt_720yyq.Interface.ToudouListener;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by NNDMII on 2017/5/8.
 */

public class ResModle<T>implements ToudouIface {

    private Retrofit retrofit;
    private ResService service;
    private SharedPreferences sp;
    private static  final String BASE_URL="http://amicool.neusoft.edu.cn/";

    private String   SID;        /*"nkgfc8ag6jq69e2uepq8u2h242";*/

    public ResModle(String SID) {
        this.SID = SID;
    }

    public ResModle(String type, String s_id) {
        if(type.equals("gson")){
            retrofit = new Retrofit.Builder().baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create()).build();

        }
        else {
            retrofit = new Retrofit.Builder().baseUrl(BASE_URL)
                    .addConverterFactory(ScalarsConverterFactory.create()).build();

        }
        service = retrofit.create(ResService.class);
        SID = s_id;
    }
//    public ResModle(String type) {
//        if(type.equals("gson")){
//            retrofit = new Retrofit.Builder().baseUrl(BASE_URL)
//                    .addConverterFactory(GsonConverterFactory.create()).build();
//
//        }
//        else {
//            retrofit = new Retrofit.Builder().baseUrl(BASE_URL)
//                    .addConverterFactory(ScalarsConverterFactory.create()).build();
//
//        }
//        service = retrofit.create(ResService.class);
//        SID = "nkgfc8ag6jq69e2uepq8u2h242";
//    }

    @Override
    public void login(String U_name, String Pass_word, final ToudouListener l) {
        Call<LoginBean> call =service.login(U_name,Pass_word);
        call.enqueue(new Callback<LoginBean>() {
            @Override
            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                l.onResponce(response.body());
            }

            @Override
            public void onFailure(Call<LoginBean> call, Throwable t) {
                l.onFail(t.toString());
            }
        });
    }

    @Override
    public void reg(String U_name, String Pass_word, String Tel_num, String Real_name, String E_mail,String Roleid, final ToudouListener l) {
        Call<String> call =service.reg(U_name,Pass_word,Real_name,Tel_num,E_mail,Roleid);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                l.onResponce(response.body());
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                l.onFail(t.toString());
            }
        });
    }
    @Override
    public void logout(String Sid, final  ToudouListener l){
        Call<String>call = service.logout(Sid);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                l.onResponce(response.body());
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                l.onFail(t.toString());
            }
        });

    }

    @Override
    public void getList(String mod, int page,final ToudouListener l) {
        switch (mod){
            case "video":
                Call<List<VideoBean>> call = service.getVideoList(mod, page,SID);
                call.enqueue(new Callback<List<VideoBean>>() {
                    @Override
                    public void onResponse(Call<List<VideoBean>> call, Response<List<VideoBean>> response) {
                        l.onResponce(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<VideoBean>> call, Throwable t) {
                        l.onFail(t.toString());
                    }
                });
                break;
            case "svideo":
                Call<List<SVideoBean>> call_s = service.getSVideoList("video",page,SID);
                call_s.enqueue(new Callback<List<SVideoBean>>() {
                    @Override
                    public void onResponse(Call<List<SVideoBean>> call, Response<List<SVideoBean>> response) {
                        l.onResponce(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<SVideoBean>> call, Throwable t) {
                        l.onFail(t.toString());
                    }
                });break;
            case "article":
                Call<List<ArticleBean>> call_art = service.getArticalList(mod,page,SID);
                call_art.enqueue(new Callback<List<ArticleBean>>() {
                    @Override
                    public void onResponse(Call<List<ArticleBean>> call, Response<List<ArticleBean>> response) {
                        l.onResponce(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<ArticleBean>> call, Throwable t) {
                        l.onFail(t.toString());
                    }
                });
                break;
            case"tware":
                Call<List<TwareBean>>call_twa = service.getTwareBeanList(mod,page,SID);
                call_twa.enqueue(new Callback<List<TwareBean>>() {
                    @Override
                    public void onResponse(Call<List<TwareBean>> call, Response<List<TwareBean>> response) {
                        l.onResponce(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<TwareBean>> call, Throwable t) {
                        l.onFail(t.toString());
                    }
                });
                break;
            case"tcase":
                Call<List<TcaseBean>>call_tca = service.getTcaseList(mod,page,SID);
                call_tca.enqueue(new Callback<List<TcaseBean>>() {
                    @Override
                    public void onResponse(Call<List<TcaseBean>> call, Response<List<TcaseBean>> response) {
                        l.onResponce(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<TcaseBean>> call, Throwable t) {
                        l.onFail(t.toString());
                    }
                });
                break;
            case"project":
                Call<List<ProjectBean>>call_pro = service.getProjectList(mod,page,SID);
                call_pro.enqueue(new Callback<List<ProjectBean>>() {
                    @Override
                    public void onResponse(Call<List<ProjectBean>> call, Response<List<ProjectBean>> response) {
                        l.onResponce(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<ProjectBean>> call, Throwable t) {
                        l.onFail(t.toString());
                    }
                });
                break;
        }
    }

    @Override
    public void getDetail(String mod, int id, String sid, ToudouListener l) {

    }



}
